package com.wu.exchange.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.wu.rabbitmq.utils.RabbitMQUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @program: RabbitMQ-Study
 * @author: 吴磊
 * @create: 2021-12-17 14:55
 */
public class ReceiveLogsDirect02 {
    private static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMQUtils.getChannel();
        //声明一个直接交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT, false, true, null);
        //声明一个队列
        String queueName = "disk";
        channel.queueDeclare(queueName, false, false, true, null);
        // 绑定交换机与队列
        channel.queueBind(queueName, EXCHANGE_NAME, "error");
        System.out.println("ReceiveLogsDirect02等待接收消息，将接收到的消息存储在磁盘......");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("ReceiveLogsDirect02接收消息并存储磁盘：" + new String(message.getBody(), StandardCharsets.UTF_8));
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };

        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("ReceiveLogsDirect02消息接收中断的回调接口");
        };

        channel.basicConsume(queueName, false, deliverCallback, cancelCallback);

    }
}
